package hydrap2p.modulemanager;

import java.util.LinkedList;
import java.util.List;

import org.apache.thrift.TException;

public class ModuleStatusRemote implements ModuleStatus {

	private final ModuleManager.Iface manager;
	private ModuleStatusType status;
	private ModuleInfo info;
	
	
	public ModuleStatusRemote(ModuleManager.Iface manager, ModuleInfo info)
	{
		this.manager=manager;
		this.info=info;
	}
	
	public void updateInfo(ModuleInfo info)
	{
		this.info=info;
		if (info.running)
			status=ModuleStatusType.STARTEDEXTER;
		else
			status=ModuleStatusType.STOPPED;
		
	}
	
	@Override
	public ModuleInfo getInfo() {
		return info;
	}
	
	@Override
	public ModuleStatusType getStatus() {
		return status;
	}

	@Override
	public void start() {
		try {
			manager.startModule(info.name, info.server);
		} catch (TException e) {
			// Failed to talk to other Manager
			e.printStackTrace();
		}
	}

	@Override
	public void stop() {
		try {
			manager.stopModule(info.name, info.server);
		} catch (TException e) {
			// Failed to talk to other Manager
			e.printStackTrace();
		}
	}
	
	@Override
	public void setAutostart(boolean autostart) {
		try {
			manager.setModuleAutostart(info.name, info.server, autostart);
		} catch (TException e) {
			// Failed to talk to other Manager
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean getAutostart() {
		return info.autostart;
	}

	@Override
	public List<String> providedServices() {
		return new LinkedList<String>();
	}

}
